import { LogicalSize, LogicalPosition } from '@tauri-apps/api/window';
import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
import { emit, listen } from '@tauri-apps/api/event';
import type { AccountConfig } from '../config/ctpConfig';

// 窗口管理器
export function useWindowManager() {

  // 打开账号添加窗口
  const openAccountAddWindow = async (editAccount?: AccountConfig) => {
    try {
      // 检查窗口是否已存在
      const existingWindow = await WebviewWindow.getByLabel('account-add');
      if (existingWindow) {
        await existingWindow.setFocus();
        if (editAccount) {
          await emit('edit-account-config', editAccount);
        }
        return existingWindow;
      }

      // 创建新的WebviewWindow
      const accountAddWindow = new WebviewWindow('account-add', {
        url: '#/account-add',
        title: '添加账号配置',
        width: 600,
        height: 340,
        minWidth: 600,
        minHeight: 340,
        resizable: true,
        decorations: false,
        // alwaysOnTop: true,
        center: true,
        skipTaskbar: false,
        visible: true,
        minimizable: true,
        maximizable: false,
        closable: true,
        // focus: true
      });

      // 等待窗口创建完成
      await accountAddWindow.once('tauri://created', async () => {

        // 如果是编辑模式，发送账号数据
        if (editAccount) {
          setTimeout(async () => {
            await emit('edit-account-config', editAccount);
          }, 1000); // 延迟发送，确保窗口完全加载
        }
      });

      // 监听窗口关闭事件
      await accountAddWindow.once('tauri://close-requested', () => {
      });

      return accountAddWindow;
    } catch (error) {
      throw error;
    }
  };

  // 监听账号配置更新事件
  const listenAccountConfigUpdated = (callback: (account: AccountConfig) => void) => {
    return listen('account-config-updated', (event: any) => {
      const data = event.payload;
      if (data && data.account) {
        callback(data.account);
      }
    });
  };

  // 关闭指定窗口
  const closeWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.close();
      }
    } catch (error) {
    }
  };

  // 获取窗口
  const getWindow = async (label: string) => {
    return await WebviewWindow.getByLabel(label);
  };

  // 设置窗口焦点
  const focusWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.setFocus();
      }
    } catch (error) {
    }
  };

  // 设置窗口大小
  const resizeWindow = async (label: string, width: number, height: number) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.setSize(new LogicalSize(width, height));
      }
    } catch (error) {
    }
  };

  // 设置窗口位置
  const moveWindow = async (label: string, x: number, y: number) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.setPosition(new LogicalPosition(x, y));
      }
    } catch (error) {
    }
  };

  // 居中窗口
  const centerWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.center();
      }
    } catch (error) {
    }
  };

  // 最小化窗口
  const minimizeWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.minimize();
      }
    } catch (error) {
    }
  };

  // 最大化窗口
  const maximizeWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.maximize();
      }
    } catch (error) {
    }
  };

  // 取消最大化窗口
  const unmaximizeWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.unmaximize();
      }
    } catch (error) {
    }
  };

  // 显示窗口
  const showWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.show();
      }
    } catch (error) {
    }
  };

  // 隐藏窗口
  const hideWindow = async (label: string) => {
    try {
      const window = await WebviewWindow.getByLabel(label);
      if (window) {
        await window.hide();
      }
    } catch (error) {
    }
  };

  return {
    openAccountAddWindow,
    listenAccountConfigUpdated,
    closeWindow,
    getWindow,
    focusWindow,
    resizeWindow,
    moveWindow,
    centerWindow,
    minimizeWindow,
    maximizeWindow,
    unmaximizeWindow,
    showWindow,
    hideWindow
  };
}
